package com.sunda.spmsorder.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsorder.entity.ShoppingCart;
import com.sunda.spmsorder.mapper.MaterialSrmLongContractMapper;
import com.sunda.spmsorder.mapper.ShoppingCartMapper;
import com.sunda.spmsorder.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2020-11-25
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    MaterialSrmLongContractMapper materialSrmLongContractMapper;

    /***
     * 根据用户工号查询购物车详细信息，以及对应物料的 货源清单 信息
     * @param userId
     * @return
     */
    @Override
    public List<Map<String, Object>> getShoppingCartList(String userId) {
        return shoppingCartMapper.getShoppingCartList(userId);
    }

    /**
     * 根据用户工号获取购物车信息-携带对应物料编号的所有长协信息
     * @param userId
     * @return
     */
    @Override
    public List<Map<String, Object>> getShoppingCartLists(String userId) {
        List<Map<String, Object>> mapList = shoppingCartMapper.getShoppingCartList(userId);
//        for (Map<String, Object> map: mapList) {
//            map.put("materialSrmSourceList", materialSrmLongContractMapper.getByMaterialNo(map.get("materialNo").toString()));
//        }
        return mapList;
    }

    /**
     * {
     * 	"userId": "90001",
     * 	"shoppingCartList": [{
     * 		"materialNo": "120000028",
     * 		"quantity": 10,
     * 		"longContractCode": "10000000006",
     * 	    "brand":"brand A"
     *        }, {
     * 		"materialNo": "1200000229",
     * 		"quantity": 10,
     * 		"longContractCode": "",
     * 	    "brand":""
     *    }]
     * }
     */
    @Override
    public ResponseResult addShoppingCart(JSONObject jsonObject) {
        if (!jsonObject.containsKey("userId") || !jsonObject.containsKey("shoppingCartList")){
            return ResponseResult.error("参数错误请重试！");
        }
        String userId = jsonObject.getString("userId");
        JSONArray array = jsonObject.getJSONArray("shoppingCartList");
        if (array.size() > 0){
            for (int i = 0; i < array.size(); i++){
                JSONObject object = array.getJSONObject(i);
                String materialNo = object.getString("materialNo");
                String quantity = object.getString("quantity");
                String longContractCode = object.getString("longContractCode");
                String brand = object.getString("brand");
                if (StringUtils.isEmpty(materialNo) || StringUtils.isEmpty(quantity)){
                    return ResponseResult.error("参数错误请重试");
                }
                /** 删除该用户指定物料编号的购物车数据，然后新增一条数据 */
                shoppingCartMapper.delete(new QueryWrapper<ShoppingCart>()
                        .eq("WORK_CODE", userId)
                        .eq("MATERIAL_NO", materialNo));

                shoppingCartMapper.insertCart(userId, materialNo, Double.parseDouble(quantity), longContractCode, brand);
            }
        }else {
            return ResponseResult.error("参数错误请重试！");
        }
        return ResponseResult.success("新增购物车数据成功");
    }

    /***
     * {
     * 	"userId": "10000",
     * 	"materialNo": ["210036864", "210023513"]
     * }
     * @param jsonObject
     * @return
     */
    @Override
    public ResponseResult deleteShoppingCart(JSONObject jsonObject) {
        if (!jsonObject.containsKey("userId") || !jsonObject.containsKey("materialNo")){
            return ResponseResult.error("参数错误请重试！");
        }
        String userId = jsonObject.getString("userId");
        JSONArray array = jsonObject.getJSONArray("materialNo");
        int item = 0;
        if (array.size() > 0){
            for (int i = 0; i < array.size(); i++){
                int j = shoppingCartMapper.delete(new QueryWrapper<ShoppingCart>()
                        .eq("WORK_CODE", userId)
                        .eq("MATERIAL_NO", array.get(i).toString()));
                item = item + j;
            }
        }
        return ResponseResult.success().add("删除数据条数：", item);
    }


    /***
     * {
     * 	"userId": "90001",
     * 	"shoppingCartList": [{
     * 		"materialNo": "120000023",
     * 		"quantity": 10,
     * 		"longContractCode": "10000000006",
     * 	    "brand":"brand A"
     *        }, {
     * 		"materialNo": "120000022",
     * 		"quantity": 10,
     * 		"longContractCode": "",
     * 	    "brand":""
     *    }]
     * }
     * @param jsonObject
     * @return
     */
    @Override
    public ResponseResult updateShoppingCart(JSONObject jsonObject) {
        if (!jsonObject.containsKey("userId") || !jsonObject.containsKey("shoppingCartList")){
            return ResponseResult.error("参数错误请重试！");
        }
        String userId = jsonObject.getString("userId");
        /** 删除指定 userId 下的购物车数据 */
        shoppingCartMapper.delete(new QueryWrapper<ShoppingCart>().eq("WORK_CODE", userId));

        List<ShoppingCart> shoppingCartList = new ArrayList<>();
        JSONArray array = jsonObject.getJSONArray("shoppingCartList");
        if (array.size() > 0){
            for (int i = 0; i < array.size(); i++){
                ShoppingCart shoppingCart = new ShoppingCart();
                JSONObject object = array.getJSONObject(i);

                shoppingCart.setUserId(userId);
                shoppingCart.setMaterialNo(object.getString("materialNo"));
                shoppingCart.setQuantity(Double.parseDouble(object.getString("quantity")));
                shoppingCart.setLongContractCode(object.getString("longContractCode"));
                shoppingCart.setBrand(object.getString("brand"));

                shoppingCartList.add(shoppingCart);
            }
            shoppingCartMapper.insertShoppingCart(shoppingCartList);
        }
        return ResponseResult.success();
    }
}
